//用户收藏
exports.user_collect = function (req, res) {

    //获取restful参数
    var restfulParams = req.routeInfo.restfulParams;

    //最终结果
    var resultData = {};

    //任务流创建
    async.waterfall([
        
            //1.用户收藏机构
            function (cb) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.searchUserOrganizationCollect + "/uc_uid/" + restfulParams.uc_uid,
                        method: 'POST',
                        data: {
                            fields: ["o_id", "o_name", "o_logo", "o_address", "o_tags"].join(",")
                        }
                    },
                    function (err, data) {
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }

                        resultData.organizationData = data.data;
                        cb();
                    }
                );

            },

            //查询机构约课
            function (cb) {

                //创建添加任务
                var tasks = [];

                //循环节点列表
                resultData.organizationData.forEach(function (item, i) {

                    //创建每一个插入任务
                    tasks.push(function (cb) {

                        //查询机构最新的一条约课课程信息
                        R.SEND_HTTP(
                            req,
                            {
                                url: CORE_APIConfig.coreAPI.search_organization_course + "/oc_oid/ " + item.o_id + "/oc_type/1/skip/0/limit/1",
                                method: 'POST',
                                data: {
                                    fields: ["oc_id", "oc_title", "oc_course_count", "oc_new_price"].join(",")
                                }
                            },
                            function (err, data) {
                                //判断是否有错误信息
                                if (err) {
                                    return cb(err, data);
                                }

                                item.courseData = data.data;

                                cb();
                            }
                        );

                    });

                });
                async.waterfall(tasks, cb);
            },
        //2、收藏的机构活动
        function (cb) {
            R.SEND_HTTP(
                req,
                {
                    url: CORE_APIConfig.coreAPI.organizationActivityCollect + "/uc_uid/" + restfulParams.uc_uid,
                    method: 'POST',
                    data: {
                        fields: ["oa_id","oa_type","oa_title","oa_title_image","l_name"].join(",")
                    }
                },
                function (err, data) {
                    //判断是否有错误信息
                    if (err) {
                        return cb(err, data);
                    }

                    resultData.organizationCollectData = data.data;
                    cb();
                }
            );

        },
        //查询对应id的活动
        function(cb){

            //创建添加任务
            var tasks = [];

            //循环节点列表
            resultData.organizationCollectData.forEach(function (item, i) {

                //创建每一个插入任务
                tasks.push(function (cb) {

                    //拼团
                    if(item.oa_type == 0){

                        R.SEND_HTTP(
                            req,
                            {
                                url    : CORE_APIConfig.coreAPI.search_bulk_purchase_activity_config + "/bpac_oaid/" + item.oa_id,
                                method : 'POST',
                                data : {
                                    fields:["bpac_id","bpac_new_price"].join(",")
                                }
                            },
                            function (err, data) {

                                //console.log(data);

                                item.price =  data.data[0].bpac_new_price;

                                cb();
                            }
                        );
                    }else if(item.oa_type == 2){

                        R.SEND_HTTP(
                            req,
                            {
                                url    : CORE_APIConfig.coreAPI.search_discounts_activity_config + "/dac_oaid/" + item.oa_id,
                                method : 'POST',
                                data : {
                                    fields:["dac_id","dac_new_price"].join(",")
                                }
                            },
                            function (err, data) {

                                //console.log(data);

                                item.price =  data.data[0].dac_new_price;

                                cb();
                            }
                        );
                        //海报
                    }else if(item.oa_type == 3){
                        item.price =0;
                    }

                });

            });

            async.waterfall(tasks, cb);

        },
        //文章收藏列表
        function (cb) {
            R.SEND_HTTP(
                req,
                {
                    url: CORE_APIConfig.coreAPI.forumContentUserCollect + "/fc_type/1/uc_uid/" + restfulParams.uc_uid,
                    method: 'POST',
                    data: {
                        fields: ["fc_utype", "fc_fcfid", "fc_id", "uc_create_time", "fc_type", "fc_answer_count", "fc_images"].join(",")
                    }
                },
                function (err, data) {
                    //判断是否有错误信息
                    if (err) {
                        return cb(err, data);
                    }

                    resultData.forumArticleData = data.data;
                    cb();
                }
            );

        },
        //查询对应id的用户
        function(cb){

            //创建添加任务
            var tasks = [];

            //循环节点列表
            resultData.forumArticleData.forEach(function (item, i) {

                //创建每一个插入任务
                tasks.push(function (cb) {

                    //拼团
                    if(item.fc_utype == 0){

                        cb();

                    }else if(item.fc_utype == 1){

                        cb();

                    }else if(item.fc_utype == 2){

                        R.SEND_HTTP(
                            req,
                            {
                                url    : CORE_APIConfig.coreAPI.userSearch + "/u_id/" + item.fc_fcfid,
                                method : 'POST',
                                data : {
                                    fields:["u_header_url","u_nickname"].join(",")
                                }
                            },
                            function (err, data) {

                                //console.log(data);

                                item.u_header_url =  data.data[0].u_header_url;
                                item.u_nickname =  data.data[0].u_nickname;

                                cb();
                            }
                        );
                    }

                });

            });

            async.waterfall(tasks, cb);

        },
        //问题收藏
        function (cb) {
            R.SEND_HTTP(
                req,
                {
                    url: CORE_APIConfig.coreAPI.forumContentUserCollect + "/fc_type/0/uc_uid/" + restfulParams.uc_uid,
                    method: 'POST',
                    data: {
                        fields: ["fc_utype", "fc_fcfid", "fc_id", "uc_create_time", "fc_type", "fc_answer_count", "fc_images"].join(",")
                    }
                },
                function (err, data) {
                    //判断是否有错误信息
                    if (err) {
                        return cb(err, data);
                    }

                    resultData.forumQuestionData = data.data;
                    cb();
                }
            );

        },
        //查询对应id的用户
        function(cb){

            //创建添加任务
            var tasks = [];

            //循环节点列表
            resultData.forumQuestionData.forEach(function (item, i) {

                //创建每一个插入任务
                tasks.push(function (cb) {

                    //拼团
                    if(item.fc_utype == 0){

                        cb();

                    }else if(item.fc_utype == 1){

                        cb();

                    }else if(item.fc_utype == 2){

                        R.SEND_HTTP(
                            req,
                            {
                                url    : CORE_APIConfig.coreAPI.userSearch + "/u_id/" + item.fc_fcfid,
                                method : 'POST',
                                data : {
                                    fields:["u_header_url","u_nickname"].join(",")
                                }
                            },
                            function (err, data) {

                                //console.log(data);

                                item.u_header_url =  data.data[0].u_header_url;
                                item.u_nickname =  data.data[0].u_nickname;

                                cb();
                            }
                        );
                    }

                });

            });

            async.waterfall(tasks, cb);

        },
        //论坛活动收藏
        function (cb) {
            R.SEND_HTTP(
                req,
                {
                    url: CORE_APIConfig.coreAPI.forumActivityCollect + "/uc_uid/" + restfulParams.uc_uid,
                    method: 'POST',
                    data: {
                        fields: ["fa_id", "fa_title", "fa_image", "fa_join_count", "fa_create_time"].join(",")
                    }
                },
                function (err, data) {
                    //判断是否有错误信息
                    if (err) {
                        return cb(err, data);
                    }

                    resultData.forumActivityData = data.data;
                    cb();
                }
            );

        }
        ], function (err, result) {

            //返回最终拼接结果
            RES.response(res, true, resultData);

        }
    );

}